package 并查集;

import java.util.Arrays;
import java.util.Scanner;


//并查集模板题
public class 合根植物 {
    static int Maxn;
    static int[] fa;

    static int find(int x) {
        if (x == fa[x]) {
            return x;
        } else {
            fa[x] = find(fa[x]);
            return fa[x]; //带路径压缩
        }
    }

    static void union(int x, int y) {
//        int tx = find(x);
//        int ty = find(y);
//        if (tx != ty) {
//            if (fa[tx] < fa[ty]) {
//                fa[tx] = fa[ty];
//            } else if (fa[tx] > fa[ty]) {
//                fa[ty] = fa[tx];
//            }
//        }
        fa[find(x)] = fa[find(y)];
    }

    static void init() { //初始化操作
        for (int i = 0; i <= Maxn; i++) {
            fa[i] = i;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //m x n 行
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        Maxn = n * m;
        fa = new int[Maxn + 1];
        init();//初始化操作
        int k = scanner.nextInt();
        for (int i = 1; i <= k; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            union(a, b);
        }
        System.out.println(Arrays.toString(fa));
        int count = 0;
        for (int j = 1; j <= n * m; j++) {
            if (fa[j] == j) {
                count++;
            }
        }
        System.out.println(count);
    }

}
